package com.css.dzj.zfjg.modules.feedback.service.impl;

import com.css.dzj.zfjg.common.util.R;
import com.css.dzj.zfjg.common.util.ServiceUtil;
import com.css.dzj.zfjg.common.util.excel.ExcelUtil;
import com.css.dzj.zfjg.modules.feedback.mapper.JgLhjgDffkMapper;
import com.css.dzj.zfjg.modules.feedback.entity.JgLhjgDffkEntity;
import com.css.dzj.zfjg.modules.feedback.entity.QJgLhjgDffkEntity;
import com.css.dzj.zfjg.modules.feedback.service.JgLhjgDffkService;
import com.css.dzj.zfjg.sys.user.entity.LoginUser;
import com.querydsl.core.QueryResults;
import com.querydsl.core.types.ExpressionUtils;
import com.querydsl.core.types.Predicate;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.apache.poi.ss.usermodel.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.transaction.Transactional;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 联合监管答复完善ServiceImpl
 */
@Service
public class JgLhjgDffkServiceImpl implements JgLhjgDffkService {

    @Autowired
    private JgLhjgDffkMapper jgLhjgDffkMapper;

    @Autowired
    private JPAQueryFactory jpaQueryFactory;

    @Autowired
    private ServiceUtil serviceUtil;


    /**
     * 添加联合监管答复信息
     * @param jgLhjgDffkEntity
     */
    @Transactional
    @Override
    public void add(JgLhjgDffkEntity jgLhjgDffkEntity, HttpServletRequest request) {
        try {
            R r = serviceUtil.getUserInfoByRequest(request);
            if(r.getCode().equals("200")) {
                LoginUser userInfo =  (LoginUser)r.get("data");
                jgLhjgDffkEntity.setCreateUser(userInfo.getLoginName());
            }
            jgLhjgDffkEntity.setUpdateTime(new Date());
            jgLhjgDffkMapper.save(jgLhjgDffkEntity);
        } catch(Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 修改监管答复完善详情
     * @param jgLhjgDffkEntity
     */
    @Transactional
    @Override
    public void update(JgLhjgDffkEntity jgLhjgDffkEntity) {

    }

    /**
     * 通过id查找监管答复完善详情
     * @param uuid
     * @return
     */
    @Override
    public JgLhjgDffkEntity findById(String uuid) {
        return jgLhjgDffkMapper.findById(uuid).get();
    }

    /**
     * 分页查询所有监管答复完善详情
     * @return
     */
    @Override
    public QueryResults<JgLhjgDffkEntity> queryAll(Pageable pageable) {
        QJgLhjgDffkEntity qJgLhjgDffkEntity = QJgLhjgDffkEntity.jgLhjgDffkEntity;
        QueryResults<JgLhjgDffkEntity> jgLhjgDffkEntityQueryResults = jpaQueryFactory.selectFrom(qJgLhjgDffkEntity)
                .orderBy(qJgLhjgDffkEntity.createTime.desc())
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchResults();
        return jgLhjgDffkEntityQueryResults;
    }

    /**
     * 多条件分页查询监管答复完善详情
     * @param pageable
     * @param unionDeptname
     * @param fkIdea
     * @param fkUnionType
     * @param superviseItemCode
     * @return
     */
    @Override
    public QueryResults<JgLhjgDffkEntity> listJgLhjgDffkEntity(Pageable pageable, String unionDeptname, String fkIdea, String fkUnionType, String superviseItemCode) {
        QJgLhjgDffkEntity qJgLhjgDffkEntity = QJgLhjgDffkEntity.jgLhjgDffkEntity;
        Predicate predicate = qJgLhjgDffkEntity.isNotNull().or(qJgLhjgDffkEntity.isNull());
        predicate = (unionDeptname == null || unionDeptname.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qJgLhjgDffkEntity.unionDeptname.like("%" + unionDeptname + "%"));
        predicate = (fkIdea == null || fkIdea.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qJgLhjgDffkEntity.fkIdea.like("%" + fkIdea + "%"));
        predicate = (fkUnionType == null || fkUnionType.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qJgLhjgDffkEntity.fkUnionType.eq(fkUnionType));
        predicate = (superviseItemCode == null || superviseItemCode.isEmpty()) ? predicate : ExpressionUtils.and(predicate, qJgLhjgDffkEntity.superviseItemCode.eq(superviseItemCode));
        QueryResults<JgLhjgDffkEntity> jgLhjgDffkEntity = jpaQueryFactory.selectFrom(qJgLhjgDffkEntity)
                .where(predicate)
                .orderBy(qJgLhjgDffkEntity.createTime.desc())
                .offset(pageable.getOffset())
                .limit(pageable.getPageSize())
                .fetchResults();
        return jgLhjgDffkEntity;
    }


    /**
     * 物理删除监管答复完善信息
     * @param jgLhjgDffkEntity
     */
    @Override
    public void delete(JgLhjgDffkEntity jgLhjgDffkEntity) {
        try {
            jgLhjgDffkMapper.delete(jgLhjgDffkEntity);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    /**
     * 查询全部
     * @return
     */
    @Override
    public List<Map> getAll(){
        return jgLhjgDffkMapper.getAll();
    }

    @Override
    public List<JgLhjgDffkEntity> getList(String taskno) {
        QJgLhjgDffkEntity q = QJgLhjgDffkEntity.jgLhjgDffkEntity;
        return jpaQueryFactory.selectFrom(q).where(q.taskNo.eq(taskno)).fetch();
    }
    /**
     * 根据taskNo获取反馈个数
     * @param taskNo
     * @return
     */
    @Override
    public String getFkCountByTaskNo(String taskNo) {
        return jgLhjgDffkMapper.getFkCountByTaskNo(taskNo);
    }
}
